// pages/articleList/articleList.js
const createRecycleContext = require('miniprogram-recycle-view');
const W = wx.getSystemInfoSync().windowWidth;
const rate = 750 / W;
const code_w1 = 690 / rate; //把每个列表的宽高转为px单位
const code_h1 = 120 / rate;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_index: 0, //当前页码
    lodeing: true,
    height_g: 0,
    list: [{
        title: '不良资产处置：是沙里淘金还是金融富矿？',
        img: 'https://api.yipaiju.com/resources/images/wawa/indexBanner_1.jpg',
        lable: '123123',
        time: '2020-03-22 15:04:54',
        url: '../article/article'
      },
      {
        title: '不良资产处置：是沙里淘金还是金融富矿？',
        img: 'https://api.yipaiju.com/resources/images/wawa/indexBanner_1.jpg',
        lable: '123123',
        time: '2020-03-22 15:04:54',
        url: '../article/article'
      },
      {
        title: '不良资产处置：是沙里淘金还是金融富矿？',
        img: 'https://api.yipaiju.com/resources/images/wawa/indexBanner_1.jpg',
        lable: '123123',
        time: '2020-03-22 15:04:54',
        url: '../article/article'
      },
    ],
    tabs: [{
      title: '标签1'
    }, {
      title: '标签2'
    }, {
      title: '标签3'
    }, {
      title: '标签4'
    }, ],
    active: 0
  },
  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.name}`,
      icon: 'none',
    });
  },
  api: function () {
    var that = this;
    //防止重复加载
    if (!that.data.lodeing) {
      return false;
    }
    that.setData({
      lodeing: false
    })
    wx.showLoading({
      title: '加载中...',
    })

    if (that.data.page_is_end) {
      wx.hideLoading()
      wx.showToast({
        title: '加载完毕',
      });
      return false;
    }
    let data =  {
      page_token: 0,
      page_size: 10
    }
    getApp().ports.api1({
    data
    }).then((res) => {
      var res = res.data;
      wx.hideLoading()
      if (data.page_token >= 1) {
        // that.onShowImg(res)
        res.results.forEach(item => {
          item.url = "../details/details?status=0&&id=" + item.id
          switch (item.stage) {
            case '变卖':
              item.stage = '秒杀'
              break;
            case '一拍':
              item.stage = '一轮'
              break;
            case '二拍':
              item.stage = '二轮'
              break;
          }
          item.tags = [{
            name: item.built_up_area_square_centimeter + '㎡'
          }, {
            name: item.discount_rate + '折'
          }, {
            name: item.stage
          }]
          item.market_price_yuan = Math.floor((item.market_price_yuan / 10000));
          item.price_yuan = Math.floor((item.price_yuan / 10000));
          item.deal_price = Math.floor((item.deal_price / 10000));
          that.data.fp.results.push(item)
        });
        res.results = that.data.fp.results
      } else {
        // that.onShowImg(res)
        res.results.forEach(item => {
          switch (item.stage) {
            case '变卖':
              item.stage = '秒杀'
              break;
            case '一拍':
              item.stage = '一轮'
              break;
            case '二拍':
              item.stage = '二轮'
              break;
          }
          item.url = "../details/details?status=0&&id=" + item.id
          item.tags = [{
            name: item.built_up_area_square_centimeter + '㎡'
          }, {
            name: item.discount_rate + '折'
          }, {
            name: item.stage
          }]
          item.market_price_yuan = Math.floor((item.market_price_yuan / 10000));
          item.deal_price = Math.floor((item.deal_price / 10000));
          item.price_yuan = Math.floor((item.price_yuan / 10000));
        })
      }
      //根据页码做排重
      if (res.next_page_token == that.data.page_index) {
        return false;
      }
      let ctx = createRecycleContext({
        id: 'recycleId', //对应wxml中设置的id
        dataKey: 'recycleList', //wxml中wx:for的值
        page: that,
        itemSize: {
          width: code_w1, //上面转换后的宽和高
          height: code_h1
        }
      })
      ctx.append(res.results) //赋值
      that.setData({
        page_index: that.data.page_index + 1, //页码+1
        page_is_end: res.results.length < res.next_page_token ? true : false, //判断返回的数据总数是否小于设置的每页总数量的值，如果小于的话说明后面每页数据了，可以停止加载了
        lodeing: true //赋值完成后设置为true，说明可以加载下一页了
      })
    }).catch(() => {
      console.error('出啥错了？');
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    let that = this;
    // 转换列表高度为px,用于设置上面标签对的高度
    wx.getSystemInfo({
      success(res) {
        that.setData({
          height_g: res.windowHeight
        })
      }
    })
    that.api()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { //上拉加载更多
    var that = this;
    var active = that.data.currentTab;
    var dataarealist = that.data.dataarealist;
    var datapricelist = that.data.datapricelist;
    var datamorelist = that.data.datamorelist;
    var type = [that.data.fp, that.data.zk];
    var myl = ['fpmyl', 'zkmyl'];
    //检查是否还有数据可以加载
    var total_size = type[active].total_size;
    var size = type[active].results.length;
    var page_token = that.data.page_token += 1
    //如果还有，则继续加载
    if (total_size > size) {
      var data = {
        page_size: 10,
        page_token: page_token
      }
      that.orderings(data); //堆叠条件
      if (that.data.orderings) { //添加排序字段
        data.orderings = that.data.orderings
      }
      that.api(data)
      //如果没有了，则停止加载，显示没有更多内容了
    } else {
      var myls = myl[active]
      that.setData({
        [myls]: true,
      })
    }
    that.setData({
      page_token: page_token
    })
  },

  /**
   * 用户点击右上角分享 
   */
  onShareAppMessage: function () {},
  navTo(e) {
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  }
})